package jmine.tec.services.environment;

import java.io.InputStream;
import java.net.URL;

import jmine.tec.environment.db.DBEnvironment;

import org.apache.commons.io.IOUtils;

import bancosys.tec.utils.db.descriptor.ReferenceDatabaseEntry;

/**
 * Postscript entry loader that supports service execution
 * 
 * @author lundberg
 */
public class ServicePostscriptEntryLoader extends AbstractRefdbPostscriptEntryLoader {

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean matches(ReferenceDatabaseEntry entry) {
        return entry.getType().equalsIgnoreCase("service");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void doLoad(DBEnvironment environment, ReferenceDatabaseEntry entry, URL url) throws Exception {
        InputStream stream = url.openStream();
        try {
            this.getServicesController(environment).executeServices(url.getFile(), stream);
        } finally {
            IOUtils.closeQuietly(stream);
        }
    }
}
